export default class SGMapSpeedChina {
  private options: any;
  private SGMap: any;
  public mapInstance: any;
  private defaultOptions: any;
  private SGMAP_APP_KEY: string;
  private SGMAP_APP_SECRET: string;
  //   private drawInstance: any;
  //   private mapLoaded: boolean;
  //   private layerId: Array<any>;
  //   private style: string;

  constructor(options: any, SGMap: any) {
    this.options = options;
    this.SGMap = SGMap;
    this.SGMAP_APP_KEY = "4b9985a37eef391f9ff32c696819f605";
    this.SGMAP_APP_SECRET = "ee7b92c92455300896b732377a662077";
    // 地图实例
    this.mapInstance = null;
    // 默认地图构造参数
    this.defaultOptions = {
      container: "SGmap", // container id
      style: "aegis://styles/aegis/Streets-v2", // style
      zoom: 11,
      center: [118.79341497586, 32.04102873840044],
      localIdeographFontFamily: "Microsoft YoHei", // 地图默认字体
      pitch: 17.5,
      minZoom: 4,
      maxZoom: 17.2,
    };
    // this.drawInstance = null;
    // this.mapLoaded = false;
    // this.layerId = [];
    // this.style = "./data/baseStyle.json";
  }
  // 初始化地图
  initMap() {
    return new Promise((resolve, reject) => {
      this.SGMap.tokenTask
        .login(this.SGMAP_APP_KEY, this.SGMAP_APP_SECRET)
        .then(() => {
          this.mapInstance = new this.SGMap.Map(
            Object.assign(this.defaultOptions, this.options)
          );
          //  放缩层级、指南针组件
          if (this.options.NavigationControl === true) {
            this.mapInstance.addControl(
              new this.SGMap.NavigationControl(),
              "bottom-right"
            );
          }
          //  2D/3D组件
          if (this.options.PitchControl === true) {
            this.mapInstance.addControl(new this.SGMap.PitchControl());
          }
          //  比例尺组件
          if (this.options.ScaleControl === true) {
            this.mapInstance.addControl(
              new this.SGMap.ScaleControl({
                maxWidth: 80,
                unit: "metric",
              }),
              "bottom-left"
            );
          }
          resolve(this.mapInstance);
        });
    });
  }
  // 获取鼠标点击经纬度
  mouseClickEvent() {
    this.mapInstance.on("click", (e: any) => {
      let lngLat = e.lngLat;
      console.log(
        "lngLat:",
        [lngLat.lng, lngLat.lat],
        "pitch:",
        this.mapInstance.getPitch()
      );
    });
  }

  // 注册事件
  //   registerFun(fun) {
  //     if (this.mapLoaded) {
  //       fun();
  //     } else {
  //       this.registerFuns.push(fun);
  //     }
  //   }

  // 移除地图
  remove() {
    this.mapInstance && this.mapInstance.remove();
  }
  // 移除地图
  drawPoint() {
    // if (drawPoint) return;
    let drawPoint: any;

    drawPoint = new this.SGMap.DrawPointHandler({
      map: this.mapInstance,
      // 开启编辑功能
      enableEdit: true,
      // 编辑数据
      featuresList: [],
    });

    // log.info("初始化成功！");
    // 事件监听
    // 开始画点
    drawPoint.on("draw.point.start", function () {
      console.log("draw.point.start");
    });
    // 画点完成
    drawPoint.on("draw.point.addPoint", function (data: any) {
      // 返回point，添加的坐标点
      console.log("draw.point.addPoint", data);
    });
    // 拖拽点结束
    drawPoint.on("edit.point.change", function (data: any) {
      // 返回point
      console.log("edit.point.change", data);
    });
    // 编辑结束
    drawPoint.on("edit.point.end", function (data: any) {
      // 返回point
      console.log("edit.point.end", data);
      // 获取所有绘图数据
      console.log(drawPoint.getFeatures());
    });
    drawPoint.startDraw();
  }

  addCircle(map: any) {
    if (!map.getLayer("demo")) {
      // 画小圆点（通过map实例身上的 addLayer 方法）
      map.addLayer({
        id: "demo",
        type: "circle",
        source: {
          type: "geojson",
          data: {
            type: "FeatureCollection",
            features: [
              {
                type: "Feature",
                geometry: {
                  type: "Point",
                  coordinates: [116, 39],
                },
              },
            ],
          },
        },
        paint: {
          "circle-radius": 10,
          "circle-color": "#fff",
          "circle-stroke-color": "#4aabf7",
          "circle-stroke-width": 5,
        },
      });

      // 设置小圆点数据
      map.getSource("demo").setData({
        type: "FeatureCollection",
        features: [
          {
            type: "Feature",
            geometry: {
              type: "Point",
              coordinates: [116, 39],
            },
            properties: {
              name: "测试点",
            },
          },
        ],
      });
    }
  }
}
// const initMap = () => {
//   const map = new SGMap.Map({
//     // 地图绑定的DOM元素ID
//     container: "SGmap",
//     // 地图样式
//     style: "aegis://styles/aegis/Streets-v2",
//     // 默认缩放层级
//     zoom: 11,
//     // 地图中心点
//     center: [116.397428, 39.90923],
//     // 地图默认字体
//     localIdeographFontFamily: "Microsoft YoHei",
//   });
//   console.log(map);
// };
